package com.example.demo.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.tools.DateTool;
import com.example.demo.sys.mapper.ForumMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

import com.example.demo.controller.Result;
import com.example.demo.sys.mapper.ForummsgMapper;
import com.example.demo.sys.entity.Forummsg;

import org.springframework.web.bind.annotation.RestController;
import com.example.demo.common.tools.BaseController;

/**
 * <p>
    *  前端控制器 chrimer
    * </p>
 *
 * @author chrimer
 * @since 2020-06-29
 */
@RestController
@RequestMapping("/sys/forummsg")
public class ForummsgController extends BaseController {
    @Autowired
    private ForummsgMapper forummsgMapper;

    @Autowired
    private ForumMapper forumMapper;


    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
        Integer pageNum,
        Integer pageSize,
        String forumId) {
        Result result = new Result();
        Page<Forummsg> page = new Page<Forummsg>(pageNum, pageSize);
    Forummsg parms = new Forummsg();
        QueryWrapper<Forummsg> warpper = new QueryWrapper<Forummsg>(parms);
        warpper.eq("forumId",forumId);
        warpper.orderByDesc("content");
        //warpper.nested(i-> i.eq("forumId",forumId).orderByDesc("createTime"));
        result.setData(forummsgMapper.selectPage(page, warpper));
        return result;
    }
    @RequestMapping(method= RequestMethod.POST,value="/save")
    public Result save(@RequestBody Forummsg entity) {
        Result result = new Result();
        if (entity.getId() == null || "".equals(entity.getId())) {
            entity.setCreateTime(DateTool.getCurrTime());
            Object etc = entity.getEtc();
            int answerNum = Integer.parseInt(String.valueOf(etc))+1;
            forumMapper.UpdateAnswer(answerNum,entity.getForumId());
           // System.out.println(entity);
            forummsgMapper.insert(entity);
        } else {//点赞数
            entity.setPraise(entity.getPraise()+1);
            forummsgMapper.updateById(entity);
        }
        return result;
    }

    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String ids) {
        Result result = new Result();
        List<String> deleteIds = new ArrayList<String>();
        for (String id : ids.split(",")) {
            deleteIds.add(id);
        }
    forummsgMapper.deleteBatchIds(deleteIds);
        return result;
    }

}
